﻿/// <reference path="../jquery-3.4.1.min.js" />
/// <reference path="../common.js" />

var uploader = null;

$(function () {
    UIInit();
    BindEvents();
    DataInit();
});


function BindEvents() {
    
    //新增
    $('#btnAdd,#btnAddBottom').click(function () {
        sysAlert.ShowIframe('userEdit.aspx', '新增人员信息',400 ,280);
    });

    //查询按钮点击事件
    $('#btnSearch').click(function () {
        GetMainList(1, $('#pager').data('pager').PageSize);
    });
}

function DataInit() {
    $('#searchBlock').bindMutliCodeDropdownList([3,4], function () {
        GetMainList(1, 15);
    });
}

function UIInit() {
    uploader = initUpload('xls,xlsx', 'btnImport', null, function () {
        var attachmentArr = uploader.attachmentList;

        $.rajax('handler/userManagement.ashx', { 'rtype': 'ImportUserData', 'fileName': attachmentArr[0].fileName }, function (retobj) {
            GetMainList(1,15);
        });
    });
}

//编辑方法
function EditOne(uid, uname) {
    sysAlert.ShowIframe('userEdit.aspx?id=' + uid, '编辑【' + uname + '】的信息', 400, 280);
}

//删除方法
function DeleteOne(deleteId) {
    sysAlert.Confirm('您确认删除选中的记录么？（此操作不可逆）', function () {
        $.rajax('handler/userManagement.ashx', { 'rtype': 'DeleteOneUser', 'id': deleteId }, function (retobj) {
            top.sysAlert.success(retobj.msg);
            GetMainList($('#pager').getPager().PageIndex, $('#pager').getPager().PageSize);
        });
    });
}

//刷新列表方法
function RefleshMainList() {
    GetMainList($('#pager').getPager().PageIndex, $('#pager').getPager().PageSize);
}

//查看用户值班列表
function ViewUserScheduleList(uid, uname) {
    sysAlert.ShowIframe('userView.aspx?id=' + uid, '【' + uname + '】的值班安排', 1000, 500);
}


//绑定列表
function GetMainList(vPageIndex, vPageSize) {
    var postData = $('#searchBlock').getData('GetPersonList');
    postData.pgIndex = vPageIndex;
    postData.pgSize = vPageSize;

    $.rajax('handler/userManagement.ashx', postData, function (retobj) {
        var dataList = retobj.data.DataList;
        //console.log(dataList);
        for (var i = 0; i < dataList.length; i++) {
            var item = dataList[i];
            item.Oper = '<a class="btn btn-info btn-sm" href="javascript:void(0)" onclick="ViewUserScheduleList(\'' + item.UserId + '\',\'' + item.UserName + '\')">查看</a>&nbsp;';
            item.Oper += '<a class="btn btn-primary btn-sm" href="javascript:void(0)" onclick="EditOne(\'' + item.UserId + '\',\'' + item.UserName + '\')">编辑</a>&nbsp;';
            item.Oper += '<a class="btn btn-danger btn-sm" href="javascript:void(0)" onclick="DeleteOne(\'' + item.UserId + '\')">删除</a>&nbsp;';

            var yesClass = '', notClass='';
            
            if (item.IsAvailableMeaning == '是') {
                yesClass = 'btn-primary';
                notClass = 'btn-outline-danger';
            }
            else {
                yesClass = 'btn-outline-primary';
                notClass = 'btn-danger';
            }

            var tempHtml = '<button type="button" onclick="ChangeUserAvailableStatus(\'' + item.UserId + '\',\'0\')" class="btn ' + notClass + ' btn-sm">否</button><button type="button" onclick="ChangeUserAvailableStatus(\'' + item.UserId + '\',\'1\')" class="btn ' + yesClass + ' btn-sm">是</button>';
            item.IsAvailableMeaning = tempHtml;
        }

        $('#dataTable').bindTable(dataList,false);
        $('#pager').setPager(retobj.data.PageInfo, GetMainList);
    });
}

//切换用户是否参与自动排班的方法
function ChangeUserAvailableStatus(uid, available) {

    $.rajax('handler/userManagement.ashx', { 'rtype': 'ChangeUserAvailable', 'UserId': uid, 'IsAvailable': available }, function (retobj) {
        top.sysAlert.success(retobj.msg);
        GetMainList($('#pager').getPager().PageIndex, $('#pager').getPager().PageSize);
    });

}
